package servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;

//FOP
import org.apache.fop.servlet.ServletContextURIResolver;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class CreatePDF extends HttpServlet {


	private static final long serialVersionUID = 1L;
	
	private FopFactory fopFactory ;
	
	private TransformerFactory tFactory ;
	
	/** URIResolver for use by this servlet */
    protected ServletContextURIResolver uriResolver;

	/**
	 * Constructor of the object.
	 */
	public CreatePDF() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			
			  
			 //Setup a buffer to obtain the content length
		    ByteArrayOutputStream out = new ByteArrayOutputStream();
		    
		    //tell the FOPFactory object where to look for resources
		    fopFactory.setURIResolver(uriResolver);


		    //Setup FOP to create a PDF and send it to the out 
		    //ByteArrayOutputStream object
		    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
		    

		    //Setup Transformer
		    //use the uriResolver object to find the XSL style sheet
		    Source xsltSrc = this.uriResolver.resolve(
		    	    "servlet-context:/xsl/peopleconvertor.xsl", null);
		    
		    //tell the TransformerFactory where to find resources  
		    tFactory.setURIResolver(uriResolver);


		    Transformer transformer = tFactory.newTransformer(xsltSrc);
		    
		    //tell the Transformer object where to find resources 
		    transformer.setURIResolver(uriResolver);

		    //Make sure the XSL transformation's result is piped through to FOP
		    Result res = new SAXResult(fop.getDefaultHandler());

		 	    
		    //use the uriResolver object to find the xml source file
		    Source xmlSrc = uriResolver.resolve(
		    	    "servlet-context:/xml/people.xml", null);
		    
		    //Start the transformation and rendering process
		    transformer.transform(xmlSrc, res);

		    //Prepare response
		    response.setContentType("application/pdf");
		    response.setContentLength(out.size());
		    
		    //Send content to Browser
		    response.getOutputStream().write(out.toByteArray());
		    response.getOutputStream().flush();
		    
			} catch (Exception e) {
				
		          e.printStackTrace(System.err);
		          
		    }
			
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet( request, response );
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		
		////specify that resources can be found in this Web application
		uriResolver = new ServletContextURIResolver(getServletContext());
		
		fopFactory = FopFactory.newInstance();
		
		tFactory = TransformerFactory.newInstance();
		
	}

}
